package co.edu.usbcali.egrapico.vista.ambient;

import co.edu.usbcali.egrapico.vista.FrmPrincipal;

public class LabelIdeOpe extends javax.swing.JLabel implements java.io.Serializable {

    private javax.swing.JTextField TextField;
    private javax.swing.JTextPane TextPane;
    private javax.swing.JButton cancel;
    private javax.swing.JPanel comment;
    private javax.swing.JInternalFrame frame;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel label;
    private javax.swing.JButton ok;
    private String toolTipText;
    private javax.swing.JMenuItem item1;
    private javax.swing.JMenuItem iten2;
    private javax.swing.JPopupMenu poput;
    private String id;
    private String simbolo;
    private boolean isObject = false;
    boolean ban = false, ban1 = false;//si no ha sido creado
    private int numLine = 0;

    /**
     * Creates a new instance of LabelIdeOpe
     */
    public LabelIdeOpe() {
        toolTipText = "";
        frame = new javax.swing.JInternalFrame("",//+(++openFrameCount) 
                false, //resizable
                true, //closable
                false, //maximizable
                true);//iconifiable
        id = "";
        simbolo = "";
        poput = new javax.swing.JPopupMenu();
        item1 = new javax.swing.JMenuItem();
        iten2 = new javax.swing.JMenuItem();
        item1.setText("Properties...");
        item1.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(java.awt.event.ActionEvent evt) {
                item1ActionPerformed(evt);
            }
        });
        poput.add(item1);

        iten2.setText("Remove");
        iten2.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(java.awt.event.ActionEvent evt) {
                item2ActionPerformed(evt);
            }
        });
        poput.add(iten2);
        setComponentPopupMenu(poput);
        label = new javax.swing.JLabel();
        TextField = new javax.swing.JTextField();
        comment = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        TextPane = new javax.swing.JTextPane();
        ok = new javax.swing.JButton();
        cancel = new javax.swing.JButton();
        frame.getContentPane().setLayout(new java.awt.FlowLayout());
        frame.setFrameIcon(null);
        frame.setMaximumSize(new java.awt.Dimension(79, 43));
        frame.setDefaultCloseOperation(javax.swing.WindowConstants.HIDE_ON_CLOSE);
        label.setText("Shape   ");
        frame.getContentPane().add(label);
        TextField.setPreferredSize(new java.awt.Dimension(180, 19));
        frame.getContentPane().add(TextField);
        comment.setLayout(null);
        comment.setBorder(javax.swing.BorderFactory.createTitledBorder("Comment"));
        comment.setPreferredSize(new java.awt.Dimension(250, 130));
        jScrollPane1.setViewportView(TextPane);

        comment.add(jScrollPane1);
        jScrollPane1.setBounds(10, 20, 230, 100);

        frame.getContentPane().add(comment);

        ok.setText("OK");
        ok.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(java.awt.event.ActionEvent evt) {
                okActionPerformed(evt);
            }
        });
        frame.getContentPane().add(ok);

        cancel.setText("Cancel");
        cancel.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelActionPerformed(evt);
            }
        });
        frame.getContentPane().add(cancel);
        frame.setBounds(60, 50, 270, 230);

    }

    private void item1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
        if (isCreate() == false) {
            Ladicionar();
        } else {
            mostrarPanel();
        }
    }

    private void item2ActionPerformed(java.awt.event.ActionEvent evt) {
        java.awt.Component suprim = getParent();
        frame.setVisible(false);
        if (suprim instanceof LPaneContext) {
            ((LPaneContext) suprim).remove(this);
            ((LPaneContext) suprim).repaint();
        } else if (suprim instanceof LPaneMethod) {
            ((PanelMethod) ((LPaneMethod) suprim).getParent().getParent().getParent().getParent().getParent().getParent()).resArgument();
            ((LPaneMethod) suprim).remove(this);
            ((LPaneMethod) suprim).repaint();
        } //definicion del removido de simbolos terminales
        else if (suprim instanceof LPaneRestriction) {
            //borramos el label ideope
            if (getIcon().toString().endsWith("value1.gif")) {
                ((PanelRestriction) ((LPaneRestriction) suprim).getParent().getParent().getParent().getParent().getParent().getParent()).resValue();
            } else if (getIcon().toString().endsWith("variable1.gif")) {
                ((PanelRestriction) ((LPaneRestriction) suprim).getParent().getParent().getParent().getParent().getParent().getParent()).resVariable();
            } else if (getIcon().toString().endsWith("argumente1.gif")) {
                ((PanelRestriction) ((LPaneRestriction) suprim).getParent().getParent().getParent().getParent().getParent().getParent()).resArgumento();
            } else if (getIcon().toString().endsWith("senderOperato1.gif")) {
                ((PanelRestriction) ((LPaneRestriction) suprim).getParent().getParent().getParent().getParent().getParent().getParent()).resSenderOperador();
            } else if (getIcon().toString().endsWith("add1.gif")) {
                ((PanelRestriction) ((LPaneRestriction) suprim).getParent().getParent().getParent().getParent().getParent().getParent()).resSuma();
            } else if (getIcon().toString().endsWith("substract1.gif")) {
                ((PanelRestriction) ((LPaneRestriction) suprim).getParent().getParent().getParent().getParent().getParent().getParent()).resResta();
            } else if (getIcon().toString().endsWith("multiply1.gif")) {
                ((PanelRestriction) ((LPaneRestriction) suprim).getParent().getParent().getParent().getParent().getParent().getParent()).resMul();
            } else if (getIcon().toString().endsWith("divide1.gif")) {
                ((PanelRestriction) ((LPaneRestriction) suprim).getParent().getParent().getParent().getParent().getParent().getParent()).resDiv();
            } else if (getIcon().toString().endsWith("equal1.gif")) {
                ((PanelRestriction) ((LPaneRestriction) suprim).getParent().getParent().getParent().getParent().getParent().getParent()).resIgual();
            } else if (getIcon().toString().endsWith("greater1.gif")) {
                ((PanelRestriction) ((LPaneRestriction) suprim).getParent().getParent().getParent().getParent().getParent().getParent()).resMayor();
            } else if (getIcon().toString().endsWith("greaterEqual1.gif")) {
                ((PanelRestriction) ((LPaneRestriction) suprim).getParent().getParent().getParent().getParent().getParent().getParent()).resMayorIgual();
            } else if (getIcon().toString().endsWith("minor1.gif")) {
                ((PanelRestriction) ((LPaneRestriction) suprim).getParent().getParent().getParent().getParent().getParent().getParent()).resMenor();
            } else if (getIcon().toString().endsWith("minorEqual1.gif")) {
                ((PanelRestriction) ((LPaneRestriction) suprim).getParent().getParent().getParent().getParent().getParent().getParent()).resMenorIgual();
            } else if (getIcon().toString().endsWith("substract1.gif")) {
                ((PanelRestriction) ((LPaneRestriction) suprim).getParent().getParent().getParent().getParent().getParent().getParent()).resDiferente();
            }
            //borramos las lineas del ideope
            ((LPaneRestriction) suprim).removerLineas(this);
            //fin de la remocion de lineas 
            ((LPaneRestriction) suprim).remove(this);
            ((LPaneRestriction) suprim).repaint();
        } else if (suprim instanceof LPaneRestrictionF) {
            //borramos el label ideope
            if (getIcon().toString().endsWith("value1.gif")) {
                ((PanelRestrictionF) ((LPaneRestrictionF) suprim).getParent().getParent().getParent().getParent().getParent().getParent()).resValue();
            } else if (getIcon().toString().endsWith("variable1.gif")) {
                ((PanelRestrictionF) ((LPaneRestrictionF) suprim).getParent().getParent().getParent().getParent().getParent().getParent()).resVariable();
            } else if (getIcon().toString().endsWith("argumente1.gif")) {
                ((PanelRestrictionF) ((LPaneRestrictionF) suprim).getParent().getParent().getParent().getParent().getParent().getParent()).resArgumento();
            } else if (getIcon().toString().endsWith("forwardOperator1.gif")) {
                ((PanelRestrictionF) ((LPaneRestrictionF) suprim).getParent().getParent().getParent().getParent().getParent().getParent()).resForwardOperador();
            } else if (getIcon().toString().endsWith("add1.gif")) {
                ((PanelRestrictionF) ((LPaneRestrictionF) suprim).getParent().getParent().getParent().getParent().getParent().getParent()).resSuma();
            } else if (getIcon().toString().endsWith("substract1.gif")) {
                ((PanelRestrictionF) ((LPaneRestrictionF) suprim).getParent().getParent().getParent().getParent().getParent().getParent()).resResta();
            } else if (getIcon().toString().endsWith("multiply1.gif")) {
                ((PanelRestrictionF) ((LPaneRestrictionF) suprim).getParent().getParent().getParent().getParent().getParent().getParent()).resMul();
            } else if (getIcon().toString().endsWith("divide1.gif")) {
                ((PanelRestrictionF) ((LPaneRestrictionF) suprim).getParent().getParent().getParent().getParent().getParent().getParent()).resDiv();
            } else if (getIcon().toString().endsWith("equal1.gif")) {
                ((PanelRestrictionF) ((LPaneRestrictionF) suprim).getParent().getParent().getParent().getParent().getParent().getParent()).resIgual();
            } else if (getIcon().toString().endsWith("greater1.gif")) {
                ((PanelRestrictionF) ((LPaneRestrictionF) suprim).getParent().getParent().getParent().getParent().getParent().getParent()).resMayor();
            } else if (getIcon().toString().endsWith("greaterEqual1.gif")) {
                ((PanelRestrictionF) ((LPaneRestrictionF) suprim).getParent().getParent().getParent().getParent().getParent().getParent()).resMayorIgual();
            } else if (getIcon().toString().endsWith("minor1.gif")) {
                ((PanelRestrictionF) ((LPaneRestrictionF) suprim).getParent().getParent().getParent().getParent().getParent().getParent()).resMenor();
            } else if (getIcon().toString().endsWith("minorEqual1.gif")) {
                ((PanelRestrictionF) ((LPaneRestrictionF) suprim).getParent().getParent().getParent().getParent().getParent().getParent()).resMenorIgual();
            } else if (getIcon().toString().endsWith("distinct1.gif")) {
                ((PanelRestrictionF) ((LPaneRestrictionF) suprim).getParent().getParent().getParent().getParent().getParent().getParent()).resDiferente();
            }
            //borramos las lineas del ideope
            ((LPaneRestrictionF) suprim).removerLineas(this);
            //fin de la remocion de lineas 
            ((LPaneRestrictionF) suprim).remove(this);
            ((LPaneRestrictionF) suprim).repaint();
        } else if (suprim instanceof LPaneRestrictionMsg) {
            if (getIcon().toString().endsWith("value.gif")) {
                ((PanelPrecedentMsg) ((LPaneRestrictionMsg) suprim).getParent().getParent().getParent().getParent().getParent().getParent()).resValue();
                ((LPaneRestrictionMsg) suprim).remove(this);
                ((LPaneRestrictionMsg) suprim).repaint();
            } else if (getIcon().toString().endsWith("variable.gif")) {
                ((PanelPrecedentMsg) ((LPaneRestrictionMsg) suprim).getParent().getParent().getParent().getParent().getParent().getParent()).resVariable();
                ((LPaneRestrictionMsg) suprim).remove(this);
                ((LPaneRestrictionMsg) suprim).repaint();
            } else if (getIcon().toString().endsWith("argument.gif")) {
                ((PanelPrecedentMsg) ((LPaneRestrictionMsg) suprim).getParent().getParent().getParent().getParent().getParent().getParent()).resArgumento();
                ((LPaneRestrictionMsg) suprim).remove(this);
                ((LPaneRestrictionMsg) suprim).repaint();
            } else if (getIcon().toString().endsWith("senderOperator.gif")) {
                ((PanelPrecedentMsg) ((LPaneRestrictionMsg) suprim).getParent().getParent().getParent().getParent().getParent().getParent()).resSenderOperador();
                ((LPaneRestrictionMsg) suprim).remove(this);
                ((LPaneRestrictionMsg) suprim).repaint();
            }
        } else if (suprim instanceof javax.swing.JPanel) {

            if (getIcon().toString().endsWith("value.gif")) {
                ((PanelPrecedentMsg) suprim.getParent().getParent().getParent().getParent()).resIdenti();
                ((javax.swing.JPanel) suprim).remove(this);
                ((javax.swing.JPanel) suprim).repaint();
            } else if (getIcon().toString().endsWith("variable.gif")) {
                ((PanelPrecedentMsg) suprim.getParent().getParent().getParent().getParent()).resIdenti();
                ((javax.swing.JPanel) suprim).remove(this);
                ((javax.swing.JPanel) suprim).repaint();
            } else if (getIcon().toString().endsWith("argument.gif")) {
                ((PanelPrecedentMsg) suprim.getParent().getParent().getParent().getParent()).resIdenti();
                ((javax.swing.JPanel) suprim).remove(this);
                ((javax.swing.JPanel) suprim).repaint();
            } else if (getIcon().toString().endsWith("senderOperator.gif")) {
                ((PanelPrecedentMsg) suprim.getParent().getParent().getParent().getParent()).resIdenti();
                ((javax.swing.JPanel) suprim).remove(this);
                ((javax.swing.JPanel) suprim).repaint();
            }
        }
    }

    private void cancelActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
        TextField.setText(getName());
        TextPane.setText(getTextToolTip().substring(getName().length() + 2));
        frame.setVisible(false);
    }

    private void okActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
        setName(TextField.getText());
        setTextToolTip(getTextField() + ": " + TextPane.getText());
        frame.setVisible(false);
    }

    public void setTextToolTip(String t) {
        toolTipText = t;
    }

    public String getTextToolTip() {
        return toolTipText;
    }

    public String getTextField() {
        return TextField.getText();
    }

    public String getTextPane() {
        return TextPane.getText();
    }

    public void setTextPane(String i) {
        TextPane.setText(i);
    }

    public void mostrarMenu(java.awt.Component c, int x, int y) {
        poput.show(c, x, y);
    }

    public void mostrarPanel() {
        frame.setVisible(true);
        try {
            frame.setSelected(true);
        } catch (java.beans.PropertyVetoException e) {
        }
    }

    public void Ladicionar() {
        TextField.setText(getName());
        FrmPrincipal.addElementtoDesKtop(frame);
        frame.setVisible(true);
        try {
            frame.setSelected(true);
        } catch (java.beans.PropertyVetoException e) {
        }
        ban = true;
    }

    public boolean isCreate() {
        return ban;
    }

    public String getId() {
        return id;
    }

    public void setId(String i) {
        id = i;
    }

    public String getSimbol() {
        return simbolo;
    }

    public void setSimbol(String i) {
        simbolo = i;
    }

    public boolean getisObject() {
        return isObject;
    }

    public void setIsOfObject(boolean i) {
        isObject = i;
    }

    public int getnumLine() {
        return numLine;
    }

    public void addLine() {
        numLine++;
    }

    public void removeLine() {
        numLine--;
    }

    public void setnumLine(int n) {
        numLine = n;
    }

    public String getNameIcon() {
        String ic = getIcon().toString();
        String icr = "";
        int i;
        for (i = ic.length(); i > 1; i--) {
            if (ic.charAt(i - 1) == '/') {
                break;
            }
        }
        icr = ic.substring(i);
        return icr;
    }
}